package org.myself.meeting.service;

import org.myself.meeting.Utils.Constant;
import org.myself.meeting.mapper.DepartmentMapper;
import org.myself.meeting.mapper.EmployeeMapper;
import org.myself.meeting.mapper.NewMessageMapper;
import org.myself.meeting.model.Employee;
import org.myself.meeting.model.EmployeeWithDep;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class EmployeeService {
    @Autowired
    EmployeeMapper employeeMapper;

    @Autowired
    NewMessageMapper nmmp;

    @Autowired
    DepartmentMapper departmentMapper;

    
    public Employee doLogin(String username, String password){
        Employee employee = employeeMapper.loadEmpByUsername(username);
        if (employee == null || !employee.getPassword().equals(password)){
            return null;
        }
        return employee;
    }

    public Integer doReg(Employee employee) {
        //密码限制
        if(employee.getPassword().length() < 8){
            return -1;
        }
        if(employee.getPhone().length() < 11){
            return -1;
        }
        //查找重复
        Employee emp = employeeMapper.loadEmpByUsername(employee.getUsername());
        if (emp != null){
            return -1;
        }
        employee.setStatus(0);//默认注册未审批
        return employeeMapper.doReg(employee);
    }

    public List<Employee> getEmpsBystatus(Integer status) {

        return employeeMapper.getEmpsBystatus(status);
    }

    public Integer updateStatus(Integer employeeid, Integer status) {
        return employeeMapper.updateStatus(employeeid,status);
    }

    public List<Employee> getAllEmps(Employee employee, Integer page, Integer pagesize) {
        page = (page-1)*pagesize;
        return employeeMapper.getAllEmps(employee,page,pagesize);
    }

    public Long getTotal(Employee employee) {
        return employeeMapper.getTotal(employee);
    }

    public List<Employee> getEmpsByDepId(Integer depId) {
        return employeeMapper.getEmpsByDepId(depId);
    }

    public Integer changPwUp(Employee emp,String password, String newpassword) {
        if(emp.getPassword().equals(password)){
            return employeeMapper.updatePassword(emp.getEmployeeid(),newpassword);
        }
        return -1;
    }

    //处理升职
    public Integer dealPromoted(Integer employeeid, Integer role, Integer status, Employee emp) {
        if(role >= Constant.THREEROlE){   //没有升职的空间了
            return -1;
        }
        role++;  //升职
        Employee empobject = employeeMapper.getEmpById(employeeid);
        if (status==1){  //通过  status是一个确认值  不是状态
            String operation = emp.getEmployeename()+" 批准 "+empobject.getEmployeename()+" 的升职请求 ";
            nmmp.addNewMessage(Constant.UPGRADEMANAGER,operation,new Date());
            return employeeMapper.updatePromoted(employeeid,status,role);
        }
        //不通过  不更改角色 改回正常的状态
        String operation = emp.getEmployeename()+" 拒绝了 "+empobject.getEmployeename()+" 的升职请求 ";
        nmmp.addNewMessage(Constant.UPGRADEMANAGER,operation,new Date());
        status =Constant.PPASSSTATUS;
        return employeeMapper.updateStatus(employeeid,status);
    }

    public Integer submitPromoted(Employee employee, Integer pupstatus,int status) {
        String operation;
        if (status == 1){
            operation = employee.getEmployeename()+" 申请升职 ";
        }else{
            operation = employee.getEmployeename()+" 取消升职 ";
        }
        nmmp.addNewMessage(Constant.UPGRADE,operation,new Date());
        return employeeMapper.updateStatus(employee.getEmployeeid(),pupstatus);
    }

    public Employee getEmpById(Integer employeeid) {
        return employeeMapper.getEmpById(employeeid);
    }

    //获取所有的员工 不分页
    public List<Employee> getAllEmpsAdmin() {
        return employeeMapper.getAllEmpsAdmin();
    }

    public Integer deleteEmpById(Integer id) {
        return employeeMapper.deleteEmpById(id);
    }

    public Integer updateEmpAll(Employee emp) {
        return employeeMapper.updateemp(emp);
    }

    public List<EmployeeWithDep> getAllEmpsByWithDep() {
        List<EmployeeWithDep>  listedp = new ArrayList<EmployeeWithDep>();
        List<Employee> listemp = employeeMapper.getAllEmpsAdmin();
        for (Employee emp: listemp) {
            if(emp.getRole() == 3){
                continue;
            }
            EmployeeWithDep empwdep = new EmployeeWithDep();
            empwdep.setEmployeename(emp.getEmployeename());
            empwdep.setDepartmentname(departmentMapper.getDepById(emp.getDepartmentid()).getDepartmentname());
            empwdep.setRole(emp.getRole() == 0?"员工":emp.getRole() == 1?"组长":"部长");
            listedp.add(empwdep);
        }
        return listedp;
    }

    public Integer updateAwardtimes(Integer employeeid,Integer awardtimes) {
        return employeeMapper.updateAwardtimes(employeeid,awardtimes);
    }

    public Integer updateAwardtimesByDepId(Integer id) {
//        Integer getawardtimes = employeeMapper.getawardtimes(id);
        return employeeMapper.updateAwardtimesByDepId(id);
    }

    public List<Employee> getEmpsByDepRole(Integer departmentid) {
        return employeeMapper.getEmpsByDepRole(departmentid,Constant.ONEROlE);
    }

    public Employee getEmpsByUsername(String username) {
        return employeeMapper.getEmpsByUsername(username);
    }
}
